<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of XmlResponse
 *
 * @author pahhan
 */
class Response_XmlResponse extends Response_Response
{
    /**
     * Returns content-type for response
     * @return string
     */
    public function getContentType()
    {
        return 'text/xml';
    }

    /**
     * Returns error element
     * @param DOMDocument $document
     * @param integer $code
     * @param string $message
     * @return DOMElement
     */
    private function getErrorElement(DOMDocument $document, $code = 0, $message = NULL)
    {
        $code = $document->createElement('code', $code);
        $message = $document->createElement('message', $message);

        $element = $document->createElement('error');

        $element->appendChild($code);
        $element->appendChild($message);

        return $element;
    }

    /**
     * Returns new XML document
     * @return \DOMDocument
     */
    private function createDocument()
    {
        return new DOMDocument('1.0', 'utf-8');
    }

    /**
     * Returns error XML
     * @param integer $code
     * @param string $message
     * @return string
     */
    public function getError($code = 0, $message = null)
    {
        $document = $this->createDocument();
        $data = $document->createElement('data');
        $document->appendChild($data);

        $data->appendChild($this->getErrorElement($document, $code, $message));

        return $document->saveXML();
    }

    /**
     * Returns update information XML
     * @param integer $count
     * @param integer $size
     * @param integer $parse_info_id
     * @return string
     */
    public function getUpdateInfo($count, $size, $parse_info_id)
    {
        $document = $this->createDocument();
        $data = $document->createElement('data');
        $document->appendChild($data);
        $data->appendChild($this->getErrorElement($document));

        $parse_info_id = $document->createElement('parse_info_id', $parse_info_id);
        $count = $document->createElement('count', $count);
        $size = $document->createElement('size', $size);

        $data->appendChild($parse_info_id);
        $data->appendChild($count);
        $data->appendChild($size);

        return $document->saveXML();
    }

    /**
     * Adds new DOM element with $name, $value to $parent
     * @param DOMNode $parent
     * @param string $name
     * @param string $value
     * @param DOMDocument $document
     */
    private function addElement(DOMNode $parent, $name, $value, DOMDocument $document)
    {
        $new = $document->createElement($name, $value);
        $parent->appendChild($new);
    }

    /**
     * Returns site DOM element from array
     * @param array $site
     * @param DOMDocument $document
     * @return DOMElement
     */
    private function getSiteElement(array $site, DOMDocument $document)
    {
        $site_el = $document->createElement('site');
        $this->addElement($site_el, 'id', $site['id'], $document);
        $this->addElement($site_el, 'url', $site['url'], $document);
        $this->addElement($site_el, 'title', $site['title'], $document);
        $this->addElement($site_el, 'comment', $site['comment'], $document);
        $this->addElement($site_el, 'image', $site['image'], $document);
        $this->addElement($site_el, 'updated_at', $site['updated_at'], $document);

        $parsers_el = $document->createElement('parsers');
        foreach( $site['parse_infos'] as $parser )
        {
            $parsers_el->appendChild($this->getParserElement($parser, $document));
        }
        $site_el->appendChild($parsers_el);

        return $site_el;
    }

    /**
     * Returns parser DOM element from array
     * @param array $parser
     * @param DOMDocument $document
     * @return DOMElement
     */
    private function getParserElement(array $parser, DOMDocument $document)
    {
        $parser_el = $document->createElement('parser');
        $this->addElement($parser_el, 'id', $parser['id'], $document);
        $this->addElement($parser_el, 'url', $parser['url'], $document);
        $this->addElement($parser_el, 'last_message_id', $parser['last_message_id'], $document);
        $this->addElement($parser_el, 'last_update', $parser['last_update'], $document);
        $this->addElement($parser_el, 'updated_at', $parser['updated_at'], $document);
        return $parser_el;
    }

    /**
     * Returns sites XML
     * @param array $sites
     * @return string
     */
    public function getSites(array $sites)
    {
        $document = $this->createDocument();
        $data = $document->createElement('data');
        $document->appendChild($data);
        $data->appendChild($this->getErrorElement($document));

        $sites_el = $document->createElement('sites');

        foreach( $sites as $site )
        {
            $sites_el->appendChild($this->getSiteElement($site, $document));
        }

        $data->appendChild($sites_el);

        return $document->saveXML();
    }

    /**
     * Returns message DOM element from array
     * @param array $message
     * @param DOMDocument $document
     * @return DOMElement
     */
    private function getMessageElement(array $message, DOMDocument $document)
    {
        $message_el = $document->createElement('message');
        $this->addElement($message_el, 'id', $message['id'], $document);
        $this->addElement($message_el, 'parse_info_id', $message['parse_info_id'], $document);
        $this->addElement($message_el, 'external_id', $message['external_id'], $document);
        $this->addElement($message_el, 'published_at', $message['published_at'], $document);
        $this->addElement($message_el, 'title', $message['title'], $document);
        $cdata = $document->createCDATASection($message['content']);
        $content = $document->createElement('content');
        $content->appendChild($cdata);
        $message_el->appendChild($content);
        //$this->addElement($message_el, 'content', $message['content'], $document);
        $this->addElement($message_el, 'tags', $message['tags'], $document);
        $this->addElement($message_el, 'updated_at', $message['updated_at'], $document);
        return $message_el;
    }

    /**
     * Returns messages XML
     * @param array $messages
     * @return string
     */
    public function getMessages(array $messages)
    {
        $document = $this->createDocument();
        $data = $document->createElement('data');
        $document->appendChild($data);
        $data->appendChild($this->getErrorElement($document));

        $messages_el = $document->createElement('messages');

        foreach( $messages as $message )
        {
            $messages_el->appendChild($this->getMessageElement($message, $document));
        }

        $data->appendChild($messages_el);

        return $document->saveXML();
    }
}

